package com.yh.autobio.modules.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.yh.autobio.R;
import com.yh.autobio.base.BaseActivity;
import com.yh.autobio.base.BaseFragment;
import com.yh.autobio.modules.bean.SimpleBackPage;

import java.lang.ref.WeakReference;

import butterknife.InjectView;

public class SimpleBackActivity extends BaseActivity {
    @InjectView(R.id.toolbar)
    Toolbar toolbar;

    public static final int DISPLAY_MONTH_IN = 0;
    public static final int DISPLAY_MONTH_OUT = 1;
    public static final int DISPLAY_HISTORY_IN = 2;
    public static final int DISPLAY_HISTORY_OUT = 3;
    public static final int DISPLAY_SHOULD_IN = 4;
    public static final int DISPLAY_SHOULD_OUT = 5;
    public static final int ADD_IN = 6;
    public static final int ADD_OUT = 7;
    public final static String BUNDLE_KEY_PAGE = "BUNDLE_KEY_PAGE";
    public final static String BUNDLE_KEY_ARGS = "BUNDLE_KEY_ARGS";
    public final static String BUNDLE_KEY_DISPLAY_TYPE_INT = "BUNDLE_KEY_DISPLAY_TYPE_INT";
    public final static String BUNDLE_KEY_DISPLAY_TYPE_STRING = "BUNDLE_KEY_DISPLAY_TYPE_STRING";
    public final static String TYPE = "TYPE";
    private static final String TAG = "FLAG_TAG";
    protected WeakReference<Fragment> mFragment;
    protected int mPageValue = -1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_simple_fragment;
    }

    @Override
    protected boolean hasBackButton() {
        return true;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        if (mPageValue == -1) {
            mPageValue = getIntent().getIntExtra(BUNDLE_KEY_PAGE, 0);
        }
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_keyboard_arrow_left_32dpdp));
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                finish();
            }
        });
        setStatusBarColor(R.color.colorPrimary);
//        toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.colorSunset));
        initFromIntent(mPageValue, getIntent());
    }

    protected void initFromIntent(int pageValue, Intent data) {
        if (data == null) {
            throw new RuntimeException(
                    "you must provide a page info to display");
        }
        SimpleBackPage page = SimpleBackPage.getPageByValue(pageValue);
        if (page == null) {
            throw new IllegalArgumentException("can not find page by value:"
                    + pageValue);
        }

        if ("int".equals(TYPE)) {
            int actionBarTitle = getIntent().getIntExtra(
                    BUNDLE_KEY_DISPLAY_TYPE_INT, -1);

            if (actionBarTitle == -1) {
                actionBarTitle = page.getTitle();
                toolbar.setTitle(actionBarTitle);
                setSupportActionBar(toolbar);
            }
        } else {
            String actionBarTitle = getIntent().getStringExtra(
                    BUNDLE_KEY_DISPLAY_TYPE_STRING);
            toolbar.setTitle(actionBarTitle);
            setSupportActionBar(toolbar);
        }

        try {
            Fragment fragment = (Fragment) page.getClz().newInstance();

            Bundle args = data.getBundleExtra(BUNDLE_KEY_ARGS);
            if (args != null) {
                fragment.setArguments(args);
            }

            FragmentTransaction trans = getSupportFragmentManager()
                    .beginTransaction();
            trans.replace(R.id.container, fragment, TAG);
            trans.commitAllowingStateLoss();

            mFragment = new WeakReference<Fragment>(fragment);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException(
                    "generate fragment error. by value:" + pageValue);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // if (mFragment.get() instanceof TweetsFragment) {
        // setActionBarTitle("话题");
        // }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // switch (item.getItemId()) {
        // case R.id.public_menu_send:
        // if (mFragment.get() instanceof TweetsFragment) {
        // sendTopic();
        // } else {
        // return super.onOptionsItemSelected(item);
        // }
        // break;
        // default:
        // break;
        // }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // if (mFragment.get() instanceof TweetsFragment) {
        // getMenuInflater().inflate(R.menu.pub_topic_menu, menu);
        // }
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 发送话题
     */
    // private void sendTopic() {
    // Bundle bundle = new Bundle();
    // bundle.putInt(TweetPubFragment.ACTION_TYPE,
    // TweetPubFragment.ACTION_TYPE_TOPIC);
    // bundle.putString("tweet_topic", "#"
    // + ((TweetsFragment) mFragment.get()).getTopic() + "# ");
    // UIHelper.showTweetActivity(this, SimpleBackPage.TWEET_PUB, bundle);
    // }
    @Override
    public void onBackPressed() {
        if (mFragment != null && mFragment.get() != null
                && mFragment.get() instanceof BaseFragment) {
            BaseFragment bf = (BaseFragment) mFragment.get();
            if (!bf.onBackPressed()) {
                super.onBackPressed();
            }
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.ACTION_DOWN
                && mFragment.get() instanceof BaseFragment) {
            ((BaseFragment) mFragment.get()).onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (outState != null) {
            String FRAGMENTS_TAG = "android:support:fragments";
            // remove掉保存的Fragment
            outState.remove(FRAGMENTS_TAG);
        }
    }
}
